package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.ColumnTable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * @author system
 * @since 2024-09-26 16:06:29
 *
 */
@Repository
public interface ColumnTableRepository extends JpaRepository<ColumnTable, Long>, JpaSpecificationExecutor<ColumnTable> {

    @Query("SELECT c FROM ColumnTable c WHERE c.parentColumnId = 0 ORDER BY c.sortOrder ASC")
    List<ColumnTable> findAllFirstLevelColumns();


    @Query(value = "SELECT * FROM column_table WHERE parent_column_id = ?1 ORDER BY sort_order ASC", nativeQuery = true)
    List<ColumnTable> findSecondLevelColumnsNative(Long parentId);

    Optional<ColumnTable> findById(Long columnId);

    List<ColumnTable> findAllById(Iterable<Long> ids);

    List<ColumnTable> findByParentColumnId(Long parentColumnId);
}